#include "TicTacToe.h"
#include "Console.h"
#include <iostream>
#include <conio.h>

using namespace Course;
using namespace std;

TicTacToe::TicTacToe(void)
{
}


TicTacToe::~TicTacToe(void)
{
}

void TicTacToe::Rita(void)
{
for(int i = 0; i < 18; i++){
	Console::gotoxy(0, i);
		cout << "|";
	Console::gotoxy(8, i);
		cout << "|";
	Console::gotoxy(16, i);	
		cout << "|";
	Console::gotoxy(24, i);
		cout << "|";
}
for(int j = 0; j < 25; j++){
	Console::gotoxy(j, 0);
		cout << "-";
	Console::gotoxy(j, 6);
		cout << "-";
	Console::gotoxy(j, 12);
		cout << "-";
	Console::gotoxy(j, 18);
		cout << "-";
}
}

void TicTacToe::Spela(void)
{
Console::gotoxy(12, 9);
	drag = 1;	
	while(1){
	
	if(_kbhit()){
	int ch = _getch();
	if(ch == 72 && Console::wherey() > 3){
		Console::gotoxy(Console::wherex(), (Console::wherey() - 6));
	}
	else if(ch == 80 && Console::wherey() < 15){
		Console::gotoxy(Console::wherex(), (Console::wherey() + 6));
	}
	else if(ch == 75 && Console::wherex() > 4){
		Console::gotoxy((Console::wherex() -8) , Console::wherey());
	}
	else if(ch == 77 && Console::wherex() < 18){
		Console::gotoxy((Console::wherex() + 8) , Console::wherey());
	}
	}
	Placera();
	Vinnare();
	}

}

void TicTacToe::Placera(void)
{
	if(_getch() == 32 && drag < 10){
		if(drag %2 == 0){
			cout << "X";
			Console::gotoxy((Console::wherex() -1), Console::wherey());
				if(Console::wherex() == 4 && Console::wherey() == 3)
					spelplan[0][0] = 'X';
				else if(Console::wherex() == 12 && Console::wherey() == 3)
					spelplan[0][1] = 'X';
				else if(Console::wherex() == 16 && Console::wherey() == 3)
					spelplan[0][2] = 'X';
				else if(Console::wherex() == 4 && Console::wherey() == 9)
					spelplan[1][0] = 'X';
				else if(Console::wherex() == 12 && Console::wherey() == 9)
					spelplan[1][1] = 'X';
				else if(Console::wherex() == 16 && Console::wherey() == 9)
					spelplan[1][2] = 'X';
				else if(Console::wherex() == 4 && Console::wherey() == 12)
					spelplan[2][0] = 'X';
				else if(Console::wherex() == 12 && Console::wherey() == 12)
					spelplan[2][1] = 'X';
				else if(Console::wherex() == 16 && Console::wherey() == 12)
					spelplan[2][2] = 'X';
		}
		else{
			cout << "O";
			Console::gotoxy((Console::wherex() -1), Console::wherey());
				if(Console::wherex() == 4 && Console::wherey() == 3)
					spelplan[0][0] = 'O';
				else if(Console::wherex() == 12 && Console::wherey() == 3)
					spelplan[0][1] = 'O';
				else if(Console::wherex() == 16 && Console::wherey() == 3)
					spelplan[0][2] = 'O';
				else if(Console::wherex() == 4 && Console::wherey() == 9)
					spelplan[1][0] = 'O';
				else if(Console::wherex() == 12 && Console::wherey() == 9)
					spelplan[1][1] = 'O';
				else if(Console::wherex() == 16 && Console::wherey() == 9)
					spelplan[1][2] = 'O';
				else if(Console::wherex() == 4 && Console::wherey() == 12)
					spelplan[2][0] = 'O';
				else if(Console::wherex() == 12 && Console::wherey() == 12)
					spelplan[2][1] = 'O';
				else if(Console::wherex() == 16 && Console::wherey() == 12)
					spelplan[2][2] = 'O';
		}
	Vinnare();
	drag +=1;
	}
}

void TicTacToe::Vinnare(void)
{
	if((spelplan[0][0] == 'X') && (spelplan[0][1] == 'X') && (spelplan [0][2] == 'X')){
	Console::gotoxy(20, 20);
	cout << "X vinner! Woho.";
	}
}

//Varje steg x-led = 8
//Varje steg y-led = 6
//Mitten x = 12 y = 9
// 120 = x
// 111 = o
// 88 = X
// 79 = O